import { User } from "../../../modeal/user.js"
import { MyDate } from "../../../utils/util.js"
import { GetTime } from "../../../utils/util.js"
const user = new User()
Page({
  data: {
    orderInfo: {},
    deskid: '',
    finallyPay: 0,
    eatInOid: '',
    totalFooNum: 0,
    posttime: ''

  },

  onLoad: function () {
    let shopInfo = wx.getStorageSync('shopInfo')
    let deskid = wx.getStorageSync('deskid')
    let eatInOid = wx.getStorageSync('eatInOid')
    this.setData({ shopInfo, deskid, eatInOid })
    this.getOrderDetail(eatInOid);  // 获取购物车详情
  },


  countFinallyPay (res) {


  },

  // 时间戳转换时间
  transformTime () {
    let time = this.data.orderInfo.posttime
    let posttime = MyDate(time)
    this.setData({ posttime })
  },

  // 计算商品件数
  countNumber (res) {
    let foo = res.details
    let temp = []
    for (let i in foo) {
      foo[i].forEach(item => {
        temp.push(item.num)
      });
    }
    let totalFooNum = temp.reduce((acc, cur) => acc + cur, 0)
    this.setData({ totalFooNum })




  },



  // 获取购物车
  getOrderDetail (eatInOid) {
    user.getOrderDetail(eatInOid).then(res => {
      this.setData({ orderInfo: res })
      this.countFinallyPay(res);   //计算支付
      this.countNumber(res);
      this.transformTime()   //时间戳转换时间
    })
  },

  // 继续点菜
  shoppingGoOn () {
    wx.redirectTo({ url: "/pages/shopping/index/index" })
  },

  // 去支付
  goPay () {
    // 选择支付方式‘
    wx.showActionSheet({
      itemList: ['余额支付', '微信支付'],
      success: res => {
        let paytype = res.tapIndex + 1
        this.pay(paytype)
      },
      fail: err => { console.log(err) }
    })
  },

  // pay
  pay (paytype) {
    let eatInOid = wx.getStorageSync('eatInOid')
    let userinfo = wx.getStorageSync('userinfo')
    let orderInfo = this.data.orderInfo
    let isEnoughMoney = userinfo.balance - orderInfo.pay_amount
    if (paytype == 1) {
      isEnoughMoney < 0 ? wx.showToast({ title: '余额不足' }) : this.payForBalance(eatInOid)
    }
    else {
      this.payForWx()

    }
  },
  // 余额支付
  payForBalance (eatInOid) {
    user.payForBalance(eatInOid).then(res => {
      // 清除缓存
      wx.removeStorageSync('eatInOid')
      wx.removeStorageSync('deskid')
      wx.redirectTo({ url: '/pages/shopping/pays/pays' })
    })
  },

  // 微信支付
  payForWx (eatInOid) {
    let amount = 0
    user.wxPay(amount, eatInOid).then(res => {
      wx.showModal({
        content: "上线对接"
      })
    })
  },

  // 微信发起支付
  payment () {
    wx.requestPayment({
      timeStamp: String(data.timeStamp),
      nonceStr: data.nonceStr,
      package: data.package,
      signType: data.signType,
      paySign: data.paySign,
      success: function (res) {
        // 清除缓存数据
        wx.removeStorageSync('eatInOid')
        wx.removeStorageSync('deskid')
        wx.showModal({
          title: '订单提示',
          content: '支付成功',
          showCancel: false,
          success: res => {
            wx.redirectTo({ url: '/pages/shopping/pays/pays' });
          },
          fail: err => {
            wx.showModal({
              title: '订单提示',
              content: '订单支付取消',
              showCancel: false,
              success: function () {
                wx.redirectTo({ url: "/pages/user/myOrder/myOrder" });
              }
            });
          },
        });
      }
    })
  }



})